#include "keyscan.h"
#include "gpio.h"
#include "stm32f0xx_ll_gpio.h"
#include "stm32f0xx_ll_bus.h"
#include "stm32f0xx_hal_pwr.h"
#include "low_power.h"


uint16_t scan_cycle=0;
uint8_t key_final_status=0;
uint8_t powers_off = 0;

uint8_t keyLeft_status=0;
uint8_t keyRight_status=0;
uint8_t Pwrkey_debounce = 0;
uint16_t press_time = 0;
uint16_t continues = 0;

uint8_t key_debounce = 0;

void set_press_time(void)
{
	press_time = 1800;
}
uint8_t return_powers(void)
{
	return powers_off;
}

void set_power_on_int(uint8_t tatus)
{
	powers_off = tatus;
}

unsigned char key_Handle(void)
{
	unsigned char p_keyleft=0;
	unsigned char p_keright=0;
	
	p_keyleft = ~(KeyLeft|0xfe);
	//p_keright = ~(KeyRight|0xfe);
	return ((p_keyleft) | (p_keright<<1));
	
}

void keyscan(void)
{
	uint8_t key_status=0;
//	static uint8_t bt_flag_back=0xff;
	
	press_key_continue();
	if(press_time == 1)
	{
		press_time = 0;
		check_the_powers_status();
	}
	
	if(scan_cycle)
		return;
	scan_cycle = 8;//20;
	
	key_status = key_Handle();

	if((key_status & KEY_LEFT_MASK)==0)
	{
		if(return_powers())
		{
			Pwrkey_debounce += 1;
			if(Pwrkey_debounce >= 50)
			{
				Pwrkey_debounce = 0;
				continues = 0;
				press_time = 0;
				check_the_powers_status(); // Continue standby for less than 1S
			}
		}
	}
	
	if(key_status != key_final_status)
	{
		key_debounce++;
		if(key_debounce < 5)return;

		key_debounce = 0;
		key_final_status = key_status;
	//	String_Printf(USART_2, &key_final_status, 1);

		///////////////////////////////////////////////////////////////////
		if((key_final_status & KEY_LEFT_MASK) != keyLeft_status)
		{
			keyLeft_status = key_final_status & KEY_LEFT_MASK;
		//	if((key_final_status&KEY_RIGHT_MASK) || (key_final_status&KEY_LEFT_MASK) || (key_final_status&KEY_DOWN_MASK))return;
			if(keyLeft_status)
			{
				continues = 1001;
			}
			else
			{
				continues = 0;
				press_time = 0;
				temperature_range_check(keyLeft_status);
			}
			
		}
		///////////////////////////////////////////////////////////////////////
/*		if((key_final_status & KEY_RIGHT_MASK) != keyRight_status)
		{
			keyRight_status = key_final_status & KEY_RIGHT_MASK;
		//	if((key_final_status&KEY_RIGHT_MASK) || (key_final_status&KEY_UP_MASK) || (key_final_status&KEY_LEFT_MASK))return;
			
			if(keyRight_status)
			{
				temperature_range_check(keyRight_status);
			}
		//	remotoe_cursor_dispose(keydown_status);
		}
*/
		
	//	String_Printf(USART_1,&key_final_status,1);
//////////////////////////////////////////////////////////////////	
	}
	
}


void scan_cycle_count(void)
{
	if(scan_cycle)scan_cycle--;
	if(continues>1)continues--;
	if(press_time>1)press_time--;
}

void press_key_continue(void)
{

	if(continues != 1)return;
	continues = 0;
	if(powers_off == 0) // Hold down the button 1S to enter sleep
	{
		key_final_status = 0;
		keyLeft_status = 0;
		set_power_on_int(POWER_OFF);
		//Enter_Sleep_Mode();
		ShutDown();
	}
	else // Hold down the button 1S to wake up
	{
		set_power_on_int(POWER_ON);
		//exti_init_beforEnterStop(0);
		__disable_irq();
		NVIC_SystemReset();
	}
}



void check_the_powers_status(void)
{
	//if(CHARGE_STATUS == 0)return;
	if(powers_off) // stanby
	{
		//Enter_Sleep_Mode();
		ShutDown();
	}
}

